package cn.hipac.qs.SimpleQuartzJob;

import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.SimpleTriggerImpl;

import java.util.Date;

/**
 * Created by xiaogu on 2017/2/23.
 */
public class SimpleQuartzJob implements Job {

    public SimpleQuartzJob() {
    }

    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("In cn.hipac.qs - executing its JOB at "
                + new Date() + " by " + context.getTrigger().getDescription());
    }

    public static void task() throws SchedulerException {
        //通过SchedulerFactory来获取一个调度器
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        //引进作业程序
        JobDetail jobDetail = new JobDetailImpl("jobDetail-s1", "jobDetailGroup-s1", SimpleQuartzJob.class);

        //new一个触发器
        SimpleTriggerImpl simpleTrigger = new SimpleTriggerImpl("simpleTrigger", "triggerGroup-s1");

        //设置作业启动时间
        long ctime = System.currentTimeMillis();
        simpleTrigger.setStartTime(new Date(ctime));

        //设置作业执行间隔10000毫秒
        simpleTrigger.setRepeatInterval(10000L);

        //设置作业执行次数
        simpleTrigger.setRepeatCount(10);

        //设置作业执行优先级默认为5
        //simpleTrigger.setPriority(10);

        //作业和触发器设置到调度器中
        scheduler.scheduleJob(jobDetail, simpleTrigger);

        //启动调度器
        scheduler.start();

//        scheduler.shutdown();
    }

    public static void main(String[] args) {
        try {
            task();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
